﻿using System;
using System.IO;
using System.Web;
using System.Web.Hosting;

namespace CoreEx.ServiceModel
{
    public class VirtualSvcPathProvider : VirtualPathProvider
    {
        public override bool FileExists(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
                return true;


            return Previous.FileExists(virtualPath);
        }

        public override bool DirectoryExists(string virtualDir)
        {
            return true;
            return base.DirectoryExists(virtualDir);
        }

        public override System.Web.Caching.CacheDependency GetCacheDependency(string virtualPath, System.Collections.IEnumerable virtualPathDependencies, DateTime utcStart)
        {
            if (IsPathVirtual(virtualPath))
                return null;
            return base.GetCacheDependency(virtualPath, virtualPathDependencies, utcStart);
        }


        private bool IsPathVirtual(string virtualPath)
        {
            String checkPath = VirtualPathUtility.ToAppRelative(virtualPath);
            return checkPath.StartsWith("~/services", StringComparison.InvariantCultureIgnoreCase);
        }

        public override VirtualFile GetFile(string virtualPath)
        {
            if (IsPathVirtual(virtualPath))
            {
                VirtualFile test = new VirtualSvcFile(virtualPath, GetServiceContractName(virtualPath));
                return test;
            }

            return base.GetFile(virtualPath);
        }



        public string GetServiceContractName(string virtualPath)
        {
            return Path.GetFileNameWithoutExtension(virtualPath);
        }


    }
    
    
    
    ///// <summary>
    ///// A <see cref="VirtualPathProvider"/> implementation that makes it 
    ///// possible to dynamically host services in Internet Information Server without the
    ///// the need for creating .svc files for each service.         
    ///// </summary>
    ///// <remarks>
    ///// The provider must be registered with the current <see cref="HostingEnvironment"/> in the 
    ///// Global.asax.cs file
    ///// <code>
    ///// <![CDATA[
    /////  protected void Application_Start(object sender, EventArgs e)
    /////  {
    /////      var pathProvider = new VirtualSvcPathProvider();
    /////      HostingEnvironment.RegisterVirtualPathProvider(pathProvider);
    /////  }    
    ///// ]]>
    ///// </code>
    ///// </remarks>
    //public class VirtualSvcPathProvider : VirtualPathProvider
    //{
    //    /// <summary>
    //    /// Gets a value that indicates whether a file exists in the virtual file system.
    //    /// </summary>
    //    /// <param name="virtualPath">The path to the virtual file.</param>
    //    /// <returns>
    //    /// true if the file exists in the virtual file system; otherwise, false.
    //    /// </returns>
    //    public override bool FileExists(string virtualPath)
    //    {
    //        //We assume that the virtual file "exists"
    //        return true;
    //    }

    //    /// <summary>
    //    /// Gets a value that indicates whether a directory exists in the virtual file system.
    //    /// </summary>
    //    /// <param name="virtualDir">The path to the virtual directory.</param>
    //    /// <returns>
    //    /// true if the directory exists in the virtual file system; otherwise, false.
    //    /// </returns>
    //    public override bool DirectoryExists(string virtualDir)
    //    {
    //        //We assume that the virtual directory "exists"
    //        return true;
    //    }


    //    /// <summary>
    //    /// Gets a virtual file from the virtual file system.
    //    /// </summary>
    //    /// <param name="virtualPath">The path to the virtual file.</param>
    //    /// <returns>
    //    /// A descendant of the <see cref="T:System.Web.Hosting.VirtualFile"/> class that represents a file in the virtual file system.
    //    /// </returns>
    //    public override VirtualFile GetFile(string virtualPath)
    //    {
    //        VirtualFile virtualSvcFile = new VirtualSvcFile(virtualPath, GetServiceContractName(virtualPath));
    //        return virtualSvcFile;
    //    }

    //    private static string GetServiceContractName(string virtualPath)
    //    {
    //        return Path.GetFileNameWithoutExtension(virtualPath);
    //    }
    //}
}